
//% color="#4a64ff" iconWidth=50 iconHeight=40
namespace ChatGLM{

      //% block="初始化智谱AI，API密钥[API] " blockType="command"

    //% API.shadow="string"   API.defl="680a228a345803facbbb06b84ffeb694.jooLHKxAc08NqyCR" 
    export function OpenAI_Iint(parameter: any, block: any) {
        let api=parameter.API.code

        Generator.addImport(`from zhipuai import ZhipuAI\nimport json`)
        Generator.addCode(`client = ZhipuAI(api_key= ${api})`)
        


    }  
    //% block="选择[MODEL]模型 top_p:[top_p]范围：(0.0,1.0) temperature:[temperature]范围：(0.0,1.0]" blockType="command"
    //% MODEL.shadow="dropdown"   MODEL.options="MODEL" 
    //% top_p.shadow="number"   top_p.defl="0.7" 
    //% temperature.shadow="number"   temperature.defl="0.9" 
    export function OpenAI_Iint1(parameter: any, block: any) {
     
        let model=parameter.MODEL.code   
        let top_p=parameter.top_p.code
        let temperature=parameter.temperature.code
        Generator.addCode(`
def askChat(question):
    response = client.chat.completions.create(
        model="${model}",
        messages=[{"role": "user", "content": question}],
        top_p=${top_p},
        temperature=${temperature},
    )
    
    return response.choices[0].message.content
    `)

    }

    //% block="输入[MESS]返回结果" blockType="reporter"
    //% MESS.shadow="string"   MESS.defl="你好，AI机器人" 
    export function OpenAI_Iintsetvolume(parameter: any, block: any) {
        let mess=parameter.MESS.code
        Generator.addCode(`askChat(${mess})`)
        
    }
}
